//
//  ImageDetailViewController.m
//  HeartTravel
//
//  Created by dlios on 15-7-6.
//  Copyright (c) 2015年 lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "ImageDetailViewController.h"
#import "HTTPTool.h"
#import "UIImageView+WebCache.h"
#import "ImageViewCollectionViewCell.h"
#import "MJRefresh.h"
#define WIDTH [UIScreen mainScreen].bounds.size.width
#define HEIGHT [UIScreen mainScreen].bounds.size.height


@interface ImageDetailViewController ()<UICollectionViewDataSource, UICollectionViewDelegate,MBProgressHUDDelegate>

@property(nonatomic, retain)MBProgressHUD *hud;

@end

@implementation ImageDetailViewController

-(void)dealloc{
    [_imageArr release];
    [_hud release];
    [_photo_infoArr release];
    [_textArr release];
    [_timeArr release];
    [super dealloc];
}

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.imageArr = [NSMutableArray array];
        self.photo_infoArr = [NSMutableArray array];
        self.textArr = [NSMutableArray array];
        self.timeArr = [NSMutableArray array];
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];

//    [self createData];
    [self createView];

    
}

- (void)createHud{
    self.hud = [[MBProgressHUD alloc] init];
    [self.hud show:YES];
    self.hud.labelText = @"加载中";
    [self.view addSubview:self.hud];
    [self.view bringSubviewToFront:self.hud];
    [self.hud release];
    self.hud.delegate = self;
}

- (void)createData{
    
    [self createHud];
    
  [HTTPTool get:[NSString stringWithFormat:@"http://api.breadtrip.com//destination/place/%@/%@/photos/?gallery_mode=1&count=18&start=%ld&sign=1024092531695f3d4b5b6f0892f391d3",self.type,self.bId,self.refresh] body:nil httpResult:JSON success:^(id result) {
      NSDictionary *dic = result;
      //       解析items数据
      NSArray *itemsArr = dic[@"items"];
      for (NSDictionary *tempDic in itemsArr) {
          
//          照片的url数组
          [self.imageArr addObject:tempDic[@"photo"]];
          
//          照片尺寸数组
          [self.photo_infoArr addObject:tempDic[@"photo_info"]];
          [self.textArr addObject:tempDic[@"text"]];

//          时间数组
          [self.timeArr addObject:tempDic[@"local_time"]];
        }
//      NSLog(@"%@",self.imageArr);
      
//      collectionView刷新数据
      UICollectionView *collectionView = (UICollectionView *)[self.view viewWithTag:1000];
      
      [collectionView footerEndRefreshing];
      
      [collectionView reloadData];
      
      self.hud.hidden = YES;
  } failure:^(NSError *error) {
      
  }];
}

//创建视图
- (void)createView{
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    flowLayout.itemSize = CGSizeMake(WIDTH, HEIGHT - 49 -64);
    flowLayout.minimumInteritemSpacing = 0;
    flowLayout.minimumLineSpacing = 0;
    flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
    
    UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT - 64 - 49) collectionViewLayout:flowLayout];
    collectionView.dataSource = self;
    collectionView.delegate = self;
    [self.view addSubview:collectionView];
    [collectionView release];
    
    collectionView.tag = 1000;
    [collectionView registerClass:[ImageViewCollectionViewCell class] forCellWithReuseIdentifier:@"reuse"];
    collectionView.pagingEnabled = YES;
    
    [collectionView addFooterWithTarget:self action:@selector(footerRefresh)];
    collectionView.contentOffset = CGPointMake(0, (HEIGHT - 49 - 64) * self.index);

}

- (void)footerRefresh{
self.refresh = self.refresh + 18;
    [self createData];
}

//collectionView的两个协议方法
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.imageArr.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    ImageViewCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"reuse" forIndexPath:indexPath];
    
//    设置图片的自适应以及居中放置
    NSDictionary *dic = self.photo_infoArr[indexPath.row];
    double imageHeight = WIDTH * [dic[@"h"] doubleValue] / [dic[@"w"] doubleValue];
    cell.myImageView.frame = CGRectMake(0, 0, WIDTH, imageHeight);
    cell.myImageView.center = cell.contentView.center;
    [cell.myImageView sd_setImageWithURL:[NSURL URLWithString:self.imageArr[indexPath.row]]];
    
//  textLabel自适应
    cell.textLabel.text = self.textArr[indexPath.row];
    NSDictionary *Dic = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:14],NSFontAttributeName, nil];
    CGRect rect = [cell.textLabel.text boundingRectWithSize:CGSizeMake(cell.contentView.frame.size.width, 0) options:NSStringDrawingUsesLineFragmentOrigin attributes:Dic context:nil];
    cell.textLabel.frame = CGRectMake(0, 0, cell.contentView.frame.size.width, rect.size.height);
    cell.myView.frame = CGRectMake(0, cell.contentView.frame.size.height - 30 - cell.textLabel.frame.size.height, cell.contentView.frame.size.width, cell.textLabel.frame.size.height + 30);
    

    
    cell.timeLabel.frame = CGRectMake(10, cell.myView.frame.size.height - 30, 150, 30);
    cell.timeLabel.text = self.timeArr[indexPath.row];
    
    return cell;
}





- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
